# -*- coding: UTF-8 -*-
import codecs
import os
import xmltodict
from typing import List
from string import Template

from utils.util import writeFile
from parse.parse_types import  PA,Module
from parse.sdb_parser import ECU

IPCP_lua = Template(
    """
-- @brief IPCP Dissector plugin
-- @author Alvin
-- @date 2019.02.21

--[[
    Proto.new(name, desc)
        name: displayed in the column of "Protocol" in the packet list
        desc: displayed as the dissection tree root in the packet details
]]
do
local p_ipcp = Proto("IPCP_Ecarx","IP Command Protocol");
local p_signal_data=Proto("IPCP_Ecarx_Sdata","IP Signal Data");
local p_pa_data=Proto("IPCP_Ecarx_PAdata","IP PA Data");
--[[
    ProtoField:
        to be used when adding items to the dissection tree
]]

--[[
    // IPCP Header Definition.
    // STACK provides |ipcp_build_header| to prepare and convert to big endian.

   typedef struct
    {
        uint16_t     service_id;          // 0x0000 - 0xFFFF
        uint16_t     operation_id;        // 0x0000 - 0xFFFF

        uint32_t     length;              // 0x00000008 - 0xFFFFFFFF
        uint32_t     senderHandleId;      // 32 bit (ServiceId8 | OpertionID8 | OpType8 | SeqNr8)

        uint8_t      protocol_version;    // 8bit, current version is 0x03
        uint8_t      op_type;             // 8bit
        uint8_t      payload_type;        // 0 - encoded, 1 - normal

        uint8_t      proc        : 1;     // 1 bit for proc flag(default 0x0), only applies for Request/SetRequest
        uint8_t      reserved    : 7;     // 7 bit for Reserved
    } __attribute__((__packed__)) ipcp_header_t;

    // IPCP Packet Definition.
    typedef struct
    {
        ipcp_header_t    header;
        uint8_t          payload[];       // payload, Actual intended message in network byte order
    } __attribute__((__packed__)) ipcp_packet_t;

    // IPCP Operation Type Definition.
    typedef enum
    {
        IPCP_OPERATION_REQUEST                  = 0x00, // Request where a response is necessary, get property from server.
        IPCP_OPERATION_SETREQUEST_NORETURN      = 0x01, // A message that does not require any response from receiver
        IPCP_OPERATION_SETREQUEST               = 0x02, // Client sets a new value and waits for the response of the server updated value.
        IPCP_OPERATION_NOTIFICATION_REQUEST     = 0x03, // A request of a notification/event callback.
        IPCP_OPERATION_RESPONSE                 = 0x04, // The Response message for IPCP_OPERATION_REQUEST
        IPCP_OPERATION_NOTIFICATION             = 0x05, // event-based notification messages
        IPCP_OPERATION_NOTIFICATION_CYCLIC      = 0x06, // messages sent based on fixed interval e.g. every 100ms

        IPCP_OPERATION_ACK                      = 0x70,
        IPCP_OPERATION_ERROR                    = 0xE0
    } ipcp_opType_t;


    // IPCP Data Type Definition.
    typedef enum
    {
        IPCP_DATA_ENCODED                       =  0x00, // Encoded message, which means the payload is using ASN.1 with PER-unaligned
        IPCP_DATA_NORMAL                        =  0x01  // Normal message
    } ipcp_dataType_t;
]]

local op_types = {
    [0x00] = "Request",
    [0x01] = "SetRequest_NoReturn",
    [0x02] = "SetRequest",
    [0x03] = "Notification_Request",
    [0x04] = "Response",
    [0x05] = "Notification",
    [0x06] = "Notification_Cyclic",
    [0x70] = "ACK",
    [0xE0] = "ERROR"
}

local service_id = {
${SIGNALSERVICE}
    [97] = "RX1",
    [98] = "RX2",
${CBPASERVICE}
 }

local operation_id = {
${SIGNALOPE}
${CBPAOPE}
 }


local signal_table={
${SignalDefinition}
}

local pa_table={
${PaDefinition}
}

local f_service_id = ProtoField.uint16("IPCP_Ecarx.service_id", "Service ID", base.HEX, service_id)
local f_operation_id = ProtoField.uint16("IPCP_Ecarx.operation_id", "Operation ID", base.HEX, operation_id)
local f_length = ProtoField.uint32("IPCP_Ecarx.length", "Length", base.DEC)
local f_sender_id = ProtoField.uint32("IPCP_Ecarx.sender_id", "SenderHandleID", base.HEX)
local f_protocol_version = ProtoField.uint8("IPCP_Ecarx.version", "Protocol Version", base.DEC)
local f_op_type = ProtoField.uint8("IPCP_Ecarx.operation_type", "Operation Type", base.HEX, op_types)
local f_payload_type = ProtoField.uint8("IPCP_Ecarx.payload_type", "Payload Type", base.DEC, { [0] = "ENCODED", [1] = "NORMAL"})
local f_proc = ProtoField.uint8("IPCP_Ecarx.proc", "Proc flag", base.DEC)
local ipcp_data = ProtoField.bytes("IPCP_Ecarx.data", "Data: ")

-- define the fileds table of this dissector(as a protoField array)
p_ipcp.fields = {
    f_service_id,
    f_operation_id,
    f_length,
    f_sender_id,
    f_protocol_version,
    f_op_type,
    f_payload_type,
    f_proc
}

p_signal_data.fields = signal_table

p_pa_data.fields = pa_table

--[[
    Data Section
]]
local data_dis = Dissector.get("data")

local function ipcp_header_dissector(buf, pkt, tree)

    --check buffer length
    local buf_len = buf:len()
    if buf_len < 16
    then
        return false
    end

    local subtree
    local pasubtree

    -- packet list columns
    pkt.cols.protocol = "IPCP"
    pkt.cols.info = "IP Command Protocol"

    --[[
        Dissection tree in packet details
    ]]
    -- tree root
    local t = tree:add(p_ipcp,buf:range(0,16))

    --child items
    --IPCP Header
    t:add(f_service_id,buf(0,2))
    local service_id = buf(0,2):uint()
    local head_id = buf(0,4):uint()
    local op_type = buf(13,1):uint()
    if op_type == 0x70
    then
        t:add(f_operation_id,buf(2,2))
    else
        t:add(f_operation_id,head_id)
    end
    t:add(f_length,buf(4,4))
    t:add(f_sender_id,buf(8,4))
    t:add(f_protocol_version,buf(12,1))
    t:add(f_op_type,buf(13,1))
    t:add(f_payload_type,buf(14,1))
    t:add(f_proc,buf(15,1))


    --data field
    if buf_len > 16
    then

        local data_len = buf:len()-16;
        if (service_id == 0x61 or service_id == 0x62 or service_id == 0x87 or service_id == 0x88) and data_len > 4
        then
            subtree=tree:add(p_signal_data,buf:range(16,data_len))
        elseif (service_id == 0x80 or service_id == 0x81 or service_id == 0x70
            or service_id == 0x78 or service_id == 0x89 or service_id == 0xA2 or service_id == 0xC7
            or service_id == 0x83 or service_id == 0xA1 or service_id == 0x8A or service_id == 0x99 or service_id == 0x9D) and data_len > 4
        then
            pasubtree=tree:add(p_pa_data,buf:range(16,data_len))
        end

        if service_id == 0x61
        then
        ${SignalRX1Struct}

        elseif service_id == 0x62
        then
        ${SignalRX2Struct}

        elseif service_id == 0x87
        then
        ${SignalCCStruct}

        elseif service_id == 0x88
        then
        ${SignalCC1000Struct}

        elseif service_id == 0x80 and head_id == ${FirstVINVinNum}
        then
        ${PAVINVinNumRX2Struct}

        elseif service_id == 0x81 and head_id == ${FirstVPVersion}
        then
        ${PAVPVersionRX2Struct}

        elseif service_id == 0x70 and head_id == ${FirstCL}
        then
        ${PACLRX2Struct}

        elseif service_id == 0x78 and head_id == ${FirstARWarningVlo}
        then
        ${PAARWarningVloRX2Struct}

        elseif service_id == 0x89 and head_id == ${FirstVFC}
        then
        ${PAVFCRX2Struct}

        elseif service_id == 0xA2 and head_id == ${FirstFD}
        then
        ${PAFDRX2Struct}

        elseif service_id == 0xC7 and head_id == ${FirstErrorReport}
        then
        ${PAErrorReportRX2Struct}

        elseif service_id == 0x83 and head_id == ${FirstPowerRes}
        then
        ${PAPowerResRX2Struct}

        elseif service_id == 0xA1 and head_id == ${FirstDiagProxy}
        then
        ${PADiagProxyRX2Struct}

        elseif service_id == 0xA4 and head_id == ${FirstFEFDSendVFCId}
        then
        ${PAFEFDSendVFCIdRX2Struct}

        elseif service_id == 0x8A and head_id == ${FirstVINDiffMsg}
        then
        ${PAVINDiffMsgRX2Struct}

        elseif service_id == 0x99 and head_id == ${FirstMcuLogPanic}
        then
        ${PAMcuLogPanicRX2Struct}

        elseif service_id == 0x9D and head_id == ${FirstAmpDiagResult}
        then
        ${PAAmpDiagResultRX2Struct}

        else
        data_dis:call(buf(16,data_len):tvb(),pkt,tree)
        end
        --local d = tree:add(buf(16,data_len),"Data")
        --d:append_text("("..data_len.."bytes)")
        --d:add(ipcp_data, buf(16,data_len))
        --d:add(buf(16,0),"[Length:"..data_len.."]")



    end

    return true
end

--[[
    Dissect Process
]]
function p_ipcp.dissector(buf,pkt,tree)
    if ipcp_header_dissector(buf,pkt,tree)
    then
        --valid IPCP diagram
    else
        data_dis:call(buf,pkt,tree)
    end
end

--[[
    Specify Protocol Port
]]
local udp_encap_table = DissectorTable.get("udp.port")
udp_encap_table:add(50335,p_ipcp)
udp_encap_table:add(50336,p_ipcp)
udp_encap_table:add(50337,p_ipcp)
udp_encap_table:add(50338,p_ipcp)
udp_encap_table:add(50339,p_ipcp)
udp_encap_table:add(50340,p_ipcp)
udp_encap_table:add(1111,p_ipcp)
udp_encap_table:add(6666,p_ipcp)
udp_encap_table:add(8888,p_ipcp)
udp_encap_table:add(8888,p_ipcp)
udp_encap_table:add(50600,p_ipcp)
udp_encap_table:add(50435,p_ipcp)
udp_encap_table:add(50601,p_ipcp)
udp_encap_table:add(50602,p_ipcp)

--[[
udp_encap_table:add(50161,p_ipcp)
udp_encap_table:add(50162,p_ipcp)
udp_encap_table:add(50164,p_ipcp)
udp_encap_table:add(50165,p_ipcp)
udp_encap_table:add(50167,p_ipcp)
udp_encap_table:add(50168,p_ipcp)
udp_encap_table:add(50169,p_ipcp)
udp_encap_table:add(50171,p_ipcp)
udp_encap_table:add(50172,p_ipcp)
udp_encap_table:add(50173,p_ipcp)
udp_encap_table:add(50174,p_ipcp)
udp_encap_table:add(50176,p_ipcp)
udp_encap_table:add(50177,p_ipcp)
]]
end
""")
# save first serviceid+opid
SIGRx1HeadList = []
SIGRx2HeadList = []

PAVFCHeadList = []
PAVINDiffMsgHeadList = []
PAMcuLogHeadList = []
PAAmpDiagResultHeadList = []
PADiagProxyHeadList = []
PAFDHeadList = []
PAFEFDHeadList = []
PAErrorReportHeadList = []
PAPOWERResHeadList = []
PACLHeadList = []
PAARWarningHeadList = []
PAVINNumHeadList = []
PAVPVerHeadList = []

# cbpa
cbpa_service_id = []
cbpa_operation_id = []
cb_operation_id = []
pa_operation_id = []
allPaDefinition = []
ipcpPAVINdict = {}
ipcpPAVinVinNumList = []
ipcpPAVINDiffMsgList = []
ipcpPAVpVersionList = []
ipcpPAClList = []
ipcpPAArWarList = []
ipcpPAVFCList = []
ipcpPAFdList = []
ipcpPAErrRepList = []
ipcpPAPowResList = []
ipcpPADiagProList = []
ipcpPAFefdVfcList = []
ipcpPAVMcuLogList = []
ipcpPAAmpDiagResultList = []

ipcpluaPAVINVinNumcontentRx = []
ipcpluaPAAmpDiagResultcontentRx = []
ipcpluaPAMcuLogPaniccontentRx = []
ipcpluaPAVINDiffMsgcontentRx = []
ipcpluaPAFEFDSendVFCIdcontentRx = []
ipcpluaPADiagProxycontentRx = []
ipcpluaPAPowerRescontentRx = []
ipcpluaPAErrorReportcontentRx = []
ipcpluaPAFDcontentRx = []
ipcpluaPAVFCcontentRx = []
ipcpluaPAARWarningVlocontentRx = []
ipcpluaPACLcontentRx = []
ipcpluaPAVPVersioncontentRx = []

# signal
ipcpluaList = []
# rx1 rx2 service id
rx_service_id = []
ASDM_service_id = []
AUD_service_id = []
BBM_service_id = []
CEM_service_id = []
HOD_service_id = []
HUD_service_id = []
SRS_service_id = []
VDDM_service_id = []
CDM_service_id = []
CCSM_service_id = []
SWSM_service_id = []
VGM_service_id = []
CC_service_id = []
CC1000_service_id = []
FPL_service_id = []

# rx1 rx2 operation id
rx_operation_id = []
ASDM_operation_id = []
AUD_operation_id = []
BBM_operation_id = []
CEM_operation_id = []
HOD_operation_id = []
HUD_operation_id = []
SRS_operation_id = []
VDDM_operation_id = []
CDM_operation_id = []
CCSM_operation_id = []
SWSM_operation_id = []
VGM_operation_id = []
CC_operation_id = []
CC1000_operation_id = []
FPL_operation_id = []

SignalDefinition = []


# RX1 ecu dict
ipcpluadictASDMRx1 = {}
ipcpluadictAUDRx1 = {}
ipcpluadictBBMRx1 = {}
ipcpluadictCEMRx1 = {}
ipcpluadictHODRx1 = {}
ipcpluadictHUDRx1 = {}
ipcpluadictSRSRx1 = {}
ipcpluadictVDDMRx1 = {}
ipcpluadictCDMRx1 = {}
ipcpluadictCCSMRx1 = {}
ipcpluadictSWSMRx1 = {}
ipcpluadictVGMRx2 = {}
ipcpluadictCCRx = {}
ipcpluadictCC1000Rx = {}
ipcpluadictFPLRx1 = {}
# RX1 ecu length dict
ipcpluaecdlengthdictASDMRx1 = {}
ipcpluaecdlengthdictAUDRx1 = {}
ipcpluaecdlengthdictBBMRx1 = {}
ipcpluaecdlengthdictCEMRx1 = {}
ipcpluaecdlengthdictHODRx1 = {}
ipcpluaecdlengthdictHUDRx1 = {}
ipcpluaecdlengthdictSRSRx1 = {}
ipcpluaecdlengthdictVDDMRx1 = {}
ipcpluaecdlengthdictCDMRx1 = {}
ipcpluaecdlengthdictCCSMRx1 = {}
ipcpluaecdlengthdictSWSMRx1 = {}
ipcpluaecdlengthdictVGMRx2 = {}
ipcpluaecdlengthdictCCRx = {}
ipcpluaecdlengthdictCC1000Rx = {}
ipcpluaecdlengthdictFPLRx1 = {}

wssignalrxstruce = []
wssignalrxstrucerx1 = []
wssignalrxstrucerx2 = []
wssignalrxstrucecc= []
wssignalrxstrucecc1000= []


def get_pa_data_lenth(pa_api: PA):
    if pa_api.data == "string":
        return pa_api.array_len
    elif pa_api.data == "uint8":
        return pa_api.array_len*4 if pa_api.array_len != 0 else 4
    elif pa_api.data == "int32_t":
        return pa_api.array_len*4 if pa_api.array_len != 0 else 4
    else:
        return 4


def get_pa_data_type(pa_api: PA):
    if pa_api.data == "string":
        return "uint8"
    else:
        return "uint32"


def gen_ipcp_object(ecusList: List[ECU], modulesList: List[Module], out_path: str):
    print("ipcp lua:->gen_ipcp_object")

# cb/pa
    for moduls in modulesList:
        count = 0
        cbpa_service_id.append("    [%s] = \"%s\"" %
                               (moduls.service_id, moduls.name))
        for cb in moduls.cb_apis:
            cb_operation_id.append(("    [0x%02x%04x] = \"%s\"," % (
                int(moduls.service_id, 10), int(cb.op_id, 10), cb.api)))

        for pa in moduls.pa_apis:
            pa_operation_id.append(("    [0x%02x%02x] = \"%s\"," % (
                int(moduls.service_id, base=10), int(pa.op_id, base=10), pa.api)))

            # PA_CL
            if moduls.service_id == str(112):
                if not PACLHeadList:
                    PACLHeadList.append("0x%02x%04x" % (int(moduls.service_id, 10), int(pa.op_id, 10)))

                ipcpPAClList.append("    "+'["'+pa.api + ' available' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' avl'+'"'+",base.DEX),")
                ipcpPAClList.append("    "+'["'+pa.api + ' data' + '"] = ProtoField.' + get_pa_data_type(
                    pa)+'("IPCP_' + str(moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' data'+'"'+",base.DEX),")
                ipcpPAClList.append("    "+'["'+pa.api + ' format' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' format'+'"'+",base.DEX),")
                ipcpPAClList.append("    "+'["'+pa.api + ' status' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' status'+'"'+",base.DEX),")

                count = 16 + count
                # ipcpluaPACLcontentRx.append('\n')
                ipcpluaPACLcontentRx.append(
                    '\n'"            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' available', count, 4))
                count = count+4
                ipcpluaPACLcontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' data', count, get_pa_data_lenth(pa)))
                # print("PA_CL_array_len:", pa.array_len)
                # print("get_pa_data_lenth:", get_pa_data_lenth(pa))
                # print("PA_CLdata:", pa.data)
                # print("PA_CL_getTypesHalType:", pa.getTypesHalType)

                count = count+get_pa_data_lenth(pa)
                ipcpluaPACLcontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' format', count, 4))
                count = count+4
                ipcpluaPACLcontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' status', count, 4))
                count = count+4-16

            # PA_AR_WarningVlo
            if moduls.service_id == str(120):
                if not PAARWarningHeadList:
                    PAARWarningHeadList.append("0x%02x%04x" % (int(moduls.service_id, 10), int(pa.op_id, 10)))

                ipcpPAArWarList.append("    "+'["'+pa.api + ' available' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' avl'+'"'+",base.DEX),")
                ipcpPAArWarList.append("    "+'["'+pa.api + ' data' + '"] = ProtoField.' + get_pa_data_type(
                    pa)+'("IPCP_' + str(moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' data'+'"'+",base.DEX),")
                ipcpPAArWarList.append("    "+'["'+pa.api + ' format' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' format'+'"'+",base.DEX),")
                ipcpPAArWarList.append("    "+'["'+pa.api + ' status' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' status'+'"'+",base.DEX),")

                count = 16 + count
                ipcpluaPAARWarningVlocontentRx.append(
                    '\n'"            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' available', count, 4))
                count = count+4
                ipcpluaPAARWarningVlocontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' data', count, get_pa_data_lenth(pa)))
                count = count+get_pa_data_lenth(pa)
                ipcpluaPAARWarningVlocontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' format', count, 4))
                count = count+4
                ipcpluaPAARWarningVlocontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' status', count, 4))
                count = count+4-16

            # PA_VIN_VinNum
            if moduls.service_id == str(128):
                if not PAVINNumHeadList:
                    PAVINNumHeadList.append("0x%02x%04x" % (int(moduls.service_id, 10), int(pa.op_id, 10)))

                ipcpPAVinVinNumList.append("    "+'["'+pa.api + ' available' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' avl'+'"'+",base.DEX),")
                ipcpPAVinVinNumList.append("    "+'["'+pa.api + ' data' + '"] = ProtoField.' + get_pa_data_type(
                    pa)+'("IPCP_' + str(moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' data'+'"'+",base.DEX),")
                ipcpPAVinVinNumList.append("    "+'["'+pa.api + ' format' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' format'+'"'+",base.DEX),")
                ipcpPAVinVinNumList.append("    "+'["'+pa.api + ' status' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' status'+'"'+",base.DEX),")

                count = 16 + count
                ipcpluaPAVINVinNumcontentRx.append(
                    '\n'"            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' available', count, 4))
                count = count+4
                ipcpluaPAVINVinNumcontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d)):append_text(\"%s\"):append_text(buf(%d,%d):string());\n" % (pa.api + ' data', count, get_pa_data_lenth(pa), '->', count, get_pa_data_lenth(pa)))
                count = count+get_pa_data_lenth(pa)
                ipcpluaPAVINVinNumcontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' format', count, 4))
                count = count+4
                ipcpluaPAVINVinNumcontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' status', count, 4))
                count = count+4-16

            # PA_VP_Version
            if moduls.service_id == str(129):
                if not PAVPVerHeadList:
                    PAVPVerHeadList.append("0x%02x%04x" % (int(moduls.service_id, 10), int(pa.op_id, 10)))

                ipcpPAVpVersionList.append("    "+'["'+pa.api + ' available' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' avl'+'"'+",base.DEX),")
                ipcpPAVpVersionList.append("    "+'["'+pa.api + ' data' + '"] = ProtoField.' + get_pa_data_type(
                    pa)+'("IPCP_' + str(moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' data'+'"'+",base.DEX),")
                ipcpPAVpVersionList.append("    "+'["'+pa.api + ' format' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' format'+'"'+",base.DEX),")
                ipcpPAVpVersionList.append("    "+'["'+pa.api + ' status' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' status'+'"'+",base.DEX),")

                count = 16 + count
                ipcpluaPAVPVersioncontentRx.append(
                    '\n'"            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' available', count, 4))
                count = count+4
                ipcpluaPAVPVersioncontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d)):append_text(\"%s\"):append_text(buf(%d,%d):string());\n" % (pa.api + ' data', count, get_pa_data_lenth(pa), '->', count, get_pa_data_lenth(pa)))
                count = count+get_pa_data_lenth(pa)
                ipcpluaPAVPVersioncontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' format', count, 4))
                count = count+4
                ipcpluaPAVPVersioncontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' status', count, 4))
                count = count+4-16

            # PA_Power_Res
            if moduls.service_id == str(131):
                if not PAPOWERResHeadList:
                    PAPOWERResHeadList.append("0x%02x%04x" % (int(moduls.service_id, 10), int(pa.op_id, 10)))

                ipcpPAPowResList.append("    "+'["'+pa.api + ' available' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' avl'+'"'+",base.DEX),")
                ipcpPAPowResList.append("    "+'["'+pa.api + ' data' + '"] = ProtoField.' + get_pa_data_type(
                    pa)+'("IPCP_' + str(moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' data'+'"'+",base.DEX),")
                ipcpPAPowResList.append("    "+'["'+pa.api + ' format' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' format'+'"'+",base.DEX),")
                ipcpPAPowResList.append("    "+'["'+pa.api + ' status' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' status'+'"'+",base.DEX),")

                count = 16 + count
                ipcpluaPAPowerRescontentRx.append(
                    '\n'"            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' available', count, 4))
                count = count+4
                ipcpluaPAPowerRescontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' data', count, get_pa_data_lenth(pa)))
                count = count+get_pa_data_lenth(pa)
                ipcpluaPAPowerRescontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' format', count, 4))
                count = count+4
                ipcpluaPAPowerRescontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' status', count, 4))
                count = count+4-16

            # PA_VFC
            if moduls.service_id == str(137):
                if not PAVFCHeadList:
                    PAVFCHeadList.append("0x%02x%04x" % (int(moduls.service_id, 10), int(pa.op_id, 10)))

                ipcpPAVFCList.append("    "+'["'+pa.api + ' available' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' avl'+'"'+",base.DEX),")
                ipcpPAVFCList.append("    "+'["'+pa.api + ' data' + '"] = ProtoField.' + get_pa_data_type(
                    pa)+'("IPCP_' + str(moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' data'+'"'+",base.DEX),")
                ipcpPAVFCList.append("    "+'["'+pa.api + ' format' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' format'+'"'+",base.DEX),")
                ipcpPAVFCList.append("    "+'["'+pa.api + ' status' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' status'+'"'+",base.DEX),")

                count = 16 + count
                ipcpluaPAVFCcontentRx.append(
                    '\n'"            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' available', count, 4))
                count = count+4
                ipcpluaPAVFCcontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' data', count, get_pa_data_lenth(pa)))
                count = count+get_pa_data_lenth(pa)
                ipcpluaPAVFCcontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' format', count, 4))
                count = count+4
                ipcpluaPAVFCcontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' status', count, 4))
                count = count+4-16

            # PA_VINDiffMsg
            if moduls.service_id == str(138):
                if not PAVINDiffMsgHeadList:
                    PAVINDiffMsgHeadList.append("0x%02x%04x" % (int(moduls.service_id, 10), int(pa.op_id, 10)))

                ipcpPAVINDiffMsgList.append("    "+'["'+pa.api + ' available' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' avl'+'"'+",base.DEX),")
                ipcpPAVINDiffMsgList.append("    "+'["'+pa.api + ' data' + '"] = ProtoField.' + get_pa_data_type(
                    pa)+'("IPCP_' + str(moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' data'+'"'+",base.DEX),")
                ipcpPAVINDiffMsgList.append("    "+'["'+pa.api + ' format' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' format'+'"'+",base.DEX),")
                ipcpPAVINDiffMsgList.append("    "+'["'+pa.api + ' status' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' status'+'"'+",base.DEX),")

                count = 16 + count
                ipcpluaPAVINDiffMsgcontentRx.append(
                    '\n'"            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' available', count, 4))
                count = count+4
                ipcpluaPAVINDiffMsgcontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' data', count, get_pa_data_lenth(pa)))
                count = count+get_pa_data_lenth(pa)
                ipcpluaPAVINDiffMsgcontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' format', count, 4))
                count = count+4
                ipcpluaPAVINDiffMsgcontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' status', count, 4))
                count = count+4-16

            # PA_McuLog_Panic
            if moduls.service_id == str(153):
                if not PAMcuLogHeadList:
                    PAMcuLogHeadList.append("0x%02x%04x" % (int(moduls.service_id, 10), int(pa.op_id, 10)))

                ipcpPAVMcuLogList.append("    "+'["'+pa.api + ' available' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' avl'+'"'+",base.DEX),")
                ipcpPAVMcuLogList.append("    "+'["'+pa.api + ' data' + '"] = ProtoField.' + get_pa_data_type(
                    pa)+'("IPCP_' + str(moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' data'+'"'+",base.DEX),")
                ipcpPAVMcuLogList.append("    "+'["'+pa.api + ' format' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' format'+'"'+",base.DEX),")
                ipcpPAVMcuLogList.append("    "+'["'+pa.api + ' status' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' status'+'"'+",base.DEX),")

                count = 16 + count
                ipcpluaPAMcuLogPaniccontentRx.append(
                    '\n'"            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' available', count, 4))
                count = count+4
                ipcpluaPAMcuLogPaniccontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d)):append_text(\"%s\"):append_text(buf(%d,%d):string());\n" % (pa.api + ' data', count, get_pa_data_lenth(pa), '->', count, get_pa_data_lenth(pa)))
                count = count+get_pa_data_lenth(pa)
                ipcpluaPAMcuLogPaniccontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' format', count, 4))
                count = count+4
                ipcpluaPAMcuLogPaniccontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' status', count, 4))
                count = count+4-16

            # PA_AmpDiagResult
            if moduls.service_id == str(157):
                if not PAAmpDiagResultHeadList:
                    PAAmpDiagResultHeadList.append("0x%02x%04x" % (int(moduls.service_id, 10), int(pa.op_id, 10)))

                ipcpPAAmpDiagResultList.append("    "+'["'+pa.api + ' available' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' avl'+'"'+",base.DEX),")
                ipcpPAAmpDiagResultList.append("    "+'["'+pa.api + ' data' + '"] = ProtoField.' + get_pa_data_type(
                    pa)+'("IPCP_' + str(moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' data'+'"'+",base.DEX),")
                ipcpPAAmpDiagResultList.append("    "+'["'+pa.api + ' format' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' format'+'"'+",base.DEX),")
                ipcpPAAmpDiagResultList.append("    "+'["'+pa.api + ' status' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' status'+'"'+",base.DEX),")

                count = 16 + count
                ipcpluaPAAmpDiagResultcontentRx.append(
                    '\n'"            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' available', count, 4))
                count = count+4
                ipcpluaPAAmpDiagResultcontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' data', count, get_pa_data_lenth(pa)))
                count = count+get_pa_data_lenth(pa)
                ipcpluaPAAmpDiagResultcontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' format', count, 4))
                count = count+4
                ipcpluaPAAmpDiagResultcontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' status', count, 4))
                count = count+4-16

            # PA_DiagProxy
            if moduls.service_id == str(161):
                if not PADiagProxyHeadList:
                    PADiagProxyHeadList.append("0x%02x%04x" % (int(moduls.service_id, 10), int(pa.op_id, 10)))

                ipcpPADiagProList.append("    "+'["'+pa.api + ' available' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' avl'+'"'+",base.DEX),")
                ipcpPADiagProList.append("    "+'["'+pa.api + ' data' + '"] = ProtoField.' + get_pa_data_type(
                    pa)+'("IPCP_' + str(moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' data'+'"'+",base.DEX),")
                ipcpPADiagProList.append("    "+'["'+pa.api + ' format' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' format'+'"'+",base.DEX),")
                ipcpPADiagProList.append("    "+'["'+pa.api + ' status' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' status'+'"'+",base.DEX),")

                count = 16 + count
                ipcpluaPADiagProxycontentRx.append(
                    '\n'"            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' available', count, 4))
                count = count+4
                ipcpluaPADiagProxycontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' data', count, get_pa_data_lenth(pa)))
                count = count+get_pa_data_lenth(pa)
                ipcpluaPADiagProxycontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' format', count, 4))
                count = count+4
                ipcpluaPADiagProxycontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' status', count, 4))
                count = count+4-16

            # PA_FD
            if moduls.service_id == str(162):
                if not PAFDHeadList:
                    PAFDHeadList.append("0x%02x%04x" % (int(moduls.service_id, 10), int(pa.op_id, 10)))

                ipcpPAFdList.append("    "+'["'+pa.api + ' available' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' avl'+'"'+",base.DEX),")
                ipcpPAFdList.append("    "+'["'+pa.api + ' data' + '"] = ProtoField.' + get_pa_data_type(
                    pa)+'("IPCP_' + str(moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' data'+'"'+",base.DEX),")
                ipcpPAFdList.append("    "+'["'+pa.api + ' format' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' format'+'"'+",base.DEX),")
                ipcpPAFdList.append("    "+'["'+pa.api + ' status' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' status'+'"'+",base.DEX),")

                count = 16 + count
                ipcpluaPAFDcontentRx.append(
                    '\n'"            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' available', count, 4))
                count = count+4
                ipcpluaPAFDcontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' data', count, get_pa_data_lenth(pa)))
                count = count+get_pa_data_lenth(pa)
                ipcpluaPAFDcontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' format', count, 4))
                count = count+4
                ipcpluaPAFDcontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' status', count, 4))
                count = count+4-16

            # PA_FEFD_Send_VFC_Id
            if moduls.service_id == str(164):
                if not PAFEFDHeadList:
                    PAFEFDHeadList.append("0x%02x%04x" % (int(moduls.service_id, 10), int(pa.op_id, 10)))

                ipcpPAFefdVfcList.append("    "+'["'+pa.api + ' available' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' avl'+'"'+",base.DEX),")
                ipcpPAFefdVfcList.append("    "+'["'+pa.api + ' data' + '"] = ProtoField.' + get_pa_data_type(
                    pa)+'("IPCP_' + str(moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' data'+'"'+",base.DEX),")
                ipcpPAFefdVfcList.append("    "+'["'+pa.api + ' format' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' format'+'"'+",base.DEX),")
                ipcpPAFefdVfcList.append("    "+'["'+pa.api + ' status' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' status'+'"'+",base.DEX),")

                count = 16 + count
                ipcpluaPAFEFDSendVFCIdcontentRx.append(
                    '\n'"            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' available', count, 4))
                count = count+4
                ipcpluaPAFEFDSendVFCIdcontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' data', count, get_pa_data_lenth(pa)))
                count = count+get_pa_data_lenth(pa)
                ipcpluaPAFEFDSendVFCIdcontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' format', count, 4))
                count = count+4
                ipcpluaPAFEFDSendVFCIdcontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' status', count, 4))
                count = count+4-16

            # PA_ErrorReport
            if moduls.service_id == str(199):
                if not PAErrorReportHeadList:
                    PAErrorReportHeadList.append("0x%02x%04x" % (int(moduls.service_id, 10), int(pa.op_id, 10)))

                ipcpPAErrRepList.append("    "+'["'+pa.api + ' available' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' avl'+'"'+",base.DEX),")
                ipcpPAErrRepList.append("    "+'["'+pa.api + ' data' + '"] = ProtoField.' + get_pa_data_type(
                    pa)+'("IPCP_' + str(moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' data'+'"'+",base.DEX),")
                ipcpPAErrRepList.append("    "+'["'+pa.api + ' format' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' format'+'"'+",base.DEX),")
                ipcpPAErrRepList.append("    "+'["'+pa.api + ' status' + '"] = ProtoField.' + 'uint32'+'("IPCP_' + str(
                    moduls.service_id) + "_RX_Data"+'",'+'"'+pa.api+' status'+'"'+",base.DEX),")

                allPaDefinition.extend(ipcpPAErrRepList)

                count = 16 + count
                ipcpluaPAErrorReportcontentRx.append(
                    '\n'"            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' available', count, 4))
                count = count+4
                ipcpluaPAErrorReportcontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' data', count, get_pa_data_lenth(pa)))
                count = count+get_pa_data_lenth(pa)
                ipcpluaPAErrorReportcontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' format', count, 4))
                count = count+4
                ipcpluaPAErrorReportcontentRx.append(
                    "            pasubtree:add(pa_table[\"%s\"],buf(%d,%d));\n" % (pa.api + ' status', count, 4))
                count = count+4-16

    cbpa_operation_id.extend(cb_operation_id)
    cbpa_operation_id.extend(pa_operation_id)

    allPaDefinition.extend(ipcpPAFefdVfcList)
    allPaDefinition.extend(ipcpPAFdList)
    allPaDefinition.extend(ipcpPADiagProList)
    allPaDefinition.extend(ipcpPAAmpDiagResultList)
    allPaDefinition.extend(ipcpPAVMcuLogList)
    allPaDefinition.extend(ipcpPAVINDiffMsgList)
    allPaDefinition.extend(ipcpPAVFCList)
    allPaDefinition.extend(ipcpPAPowResList)
    allPaDefinition.extend(ipcpPAClList)
    allPaDefinition.extend(ipcpPAArWarList)
    allPaDefinition.extend(ipcpPAVinVinNumList)
    allPaDefinition.extend(ipcpPAVpVersionList)

    # signal
    index = 0
    for ecu in ecusList:
        ipcpluacontentRx = []
        ipcpluaeculengthRx = 0

        # rx1
        if ecu.name == "ASDM":
            strTemp = "ASDM"+"RX"
            ASDM_service_id.append('    [%d] = "%s"' %
                                   (ecu.serviceId, ecu.name))

            for signal in ecu.rxPackage.signals:
                index = index+1
                ASDM_operation_id.append("    [0x%02x%02x] = \"%s\"," % (
                    ecu.serviceId, index, signal.name))
                SignalDefinition.append("    "+'["'+signal.name + '"] = ProtoField.' + str(signal.type).replace(
                    "_t", "").replace("TypeEnum.", "")+'("IPCP_' + ecu.name + "_RX_Data"+'",'+'"'+signal.name+'"'+",base.DEX),")

                if ipcpluadictASDMRx1:
                    ipcpluaeculengthASDMRx1 = ipcpluaecdlengthdictASDMRx1.get(
                        ecu.name)
                    ipcpluacontentRx.append("            subtree:add(signal_table[\"%s\"],buf(%s + %d,%d));\n" % (
                        signal.name, strTemp, ipcpluaeculengthASDMRx1, parsesignallen(str(signal.type).replace("TypeEnum.", ""))))
                    ipcpluadictASDMRx1[ecu.name] = ipcpluacontentRx

                    ipcpluaeculengthASDMRx1 += parsesignallen(
                        str(signal.type).replace("TypeEnum.", ""))
                    ipcpluaecdlengthdictASDMRx1[ecu.name] = ipcpluaeculengthASDMRx1

                else:
                    ipcpluaeculengthASDMRx1 = 16
                    ipcpluaeculengthASDMRx1 += 6
                    ipcpluacontentRx.append("    subtree:add(signal_table[\"%s\"],buf(%s + %d,%d));\n" % (
                        signal.name, strTemp, ipcpluaeculengthASDMRx1, parsesignallen(str(signal.type).replace("TypeEnum.", ""))))
                    ipcpluadictASDMRx1[ecu.name] = ipcpluacontentRx

                    ipcpluaeculengthASDMRx1 += parsesignallen(
                        str(signal.type).replace("TypeEnum.", ""))
                    ipcpluaecdlengthdictASDMRx1[ecu.name] = ipcpluaeculengthASDMRx1

        if ecu.name == "AUD":
            strTemp = "AUD"+"RX"
            AUD_service_id.append('    [%d] = "%s"' %
                                  (ecu.serviceId, ecu.name))
            for signal in ecu.rxPackage.signals:
                index = index+1
                AUD_operation_id.append("    [0x%02x%02x] = \"%s\"," % (
                    ecu.serviceId, index, signal.name))
                SignalDefinition.append("    "+'["'+signal.name + '"] = ProtoField.' + str(signal.type).replace(
                    "_t", "").replace("TypeEnum.", "")+'("IPCP_' + ecu.name + "_RX_Data"+'",'+'"'+signal.name+'"'+",base.DEX),")

                if ipcpluadictAUDRx1:
                    ipcpluaeculengthAUDRx1 = ipcpluaecdlengthdictAUDRx1.get(
                        ecu.name)
                    ipcpluacontentRx.append("            subtree:add(signal_table[\"%s\"],buf(%s + %d,%d));\n" % (
                        signal.name, strTemp, ipcpluaeculengthAUDRx1, parsesignallen(str(signal.type).replace("TypeEnum.", ""))))
                    ipcpluadictAUDRx1[ecu.name] = ipcpluacontentRx

                    ipcpluaeculengthAUDRx1 += parsesignallen(
                        str(signal.type).replace("TypeEnum.", ""))
                    ipcpluaecdlengthdictAUDRx1[ecu.name] = ipcpluaeculengthAUDRx1
                else:
                    ipcpluaeculengthAUDRx1 = 0
                    ipcpluaeculengthAUDRx1 += 6
                    ipcpluacontentRx.append("            subtree:add(signal_table[\"%s\"],buf(%s + %d,%d));\n" % (
                        signal.name, strTemp, ipcpluaeculengthAUDRx1, parsesignallen(str(signal.type).replace("TypeEnum.", ""))))
                    ipcpluadictAUDRx1[ecu.name] = ipcpluacontentRx

                    ipcpluaeculengthRx += parsesignallen(
                        str(signal.type).replace("TypeEnum.", ""))
                    ipcpluaecdlengthdictAUDRx1[ecu.name] = ipcpluaeculengthRx + ipcpluaeculengthAUDRx1

        if ecu.name == "BBM":
            strTemp = "BBM"+"RX"
            BBM_service_id.append('    [%d] = "%s"' %
                                  (ecu.serviceId, ecu.name))
            for signal in ecu.rxPackage.signals:
                index = index+1
                BBM_operation_id.append("    [0x%02x%02x] = \"%s\"," % (
                    ecu.serviceId, index, signal.name))
                SignalDefinition.append("    "+'["'+signal.name + '"] = ProtoField.' + str(signal.type).replace(
                    "_t", "").replace("TypeEnum.", "")+'("IPCP_' + ecu.name + "_RX_Data"+'",'+'"'+signal.name+'"'+",base.DEX),")

                if ipcpluadictBBMRx1:
                    ipcpluaeculengthBBMRx1 = ipcpluaecdlengthdictBBMRx1.get(
                        ecu.name)
                    ipcpluacontentRx.append("            subtree:add(signal_table[\"%s\"],buf(%s + %d,%d));\n" % (
                        signal.name, strTemp, ipcpluaeculengthBBMRx1, parsesignallen(str(signal.type).replace("TypeEnum.", ""))))
                    ipcpluadictBBMRx1[ecu.name] = ipcpluacontentRx

                    ipcpluaeculengthBBMRx1 += parsesignallen(
                        str(signal.type).replace("TypeEnum.", ""))
                    ipcpluaecdlengthdictBBMRx1[ecu.name] = ipcpluaeculengthBBMRx1
                else:
                    ipcpluaeculengthBBMRx1 = 0
                    ipcpluaeculengthBBMRx1 += 6
                    ipcpluacontentRx.append("            subtree:add(signal_table[\"%s\"],buf(%s + %d,%d));\n" % (
                        signal.name, strTemp, ipcpluaeculengthBBMRx1, parsesignallen(str(signal.type).replace("TypeEnum.", ""))))
                    ipcpluadictBBMRx1[ecu.name] = ipcpluacontentRx

                    ipcpluaeculengthRx += parsesignallen(
                        str(signal.type).replace("TypeEnum.", ""))
                    ipcpluaecdlengthdictBBMRx1[ecu.name] = ipcpluaeculengthRx + ipcpluaeculengthBBMRx1

        if ecu.name == "CEM":
            strTemp = "CEM"+"RX"
            CEM_service_id.append('    [%d] = "%s"' %
                                  (ecu.serviceId, ecu.name))
            for signal in ecu.rxPackage.signals:
                index = index+1
                CEM_operation_id.append("    [0x%02x%02x] = \"%s\"," % (
                    ecu.serviceId, index, signal.name))
                SignalDefinition.append("    "+'["'+signal.name + '"] = ProtoField.' + str(signal.type).replace(
                    "_t", "").replace("TypeEnum.", "")+'("IPCP_' + ecu.name + "_RX_Data"+'",'+'"'+signal.name+'"'+",base.DEX),")

                if ipcpluadictCEMRx1:
                    ipcpluaeculengthCEMRx1 = ipcpluaecdlengthdictCEMRx1.get(
                        ecu.name)
                    ipcpluacontentRx.append("            subtree:add(signal_table[\"%s\"],buf(%s + %d,%d));\n" % (
                        signal.name, strTemp, ipcpluaeculengthCEMRx1, parsesignallen(str(signal.type).replace("TypeEnum.", ""))))
                    ipcpluadictCEMRx1[ecu.name] = ipcpluacontentRx

                    ipcpluaeculengthCEMRx1 += parsesignallen(
                        str(signal.type).replace("TypeEnum.", ""))
                    ipcpluaecdlengthdictCEMRx1[ecu.name] = ipcpluaeculengthCEMRx1
                else:
                    ipcpluaeculengthCEMRx1 = 0
                    ipcpluaeculengthCEMRx1 += 6
                    ipcpluacontentRx.append("            subtree:add(signal_table[\"%s\"],buf(%s + %d,%d));\n" % (
                        signal.name, strTemp, ipcpluaeculengthCEMRx1, parsesignallen(str(signal.type).replace("TypeEnum.", ""))))
                    ipcpluadictCEMRx1[ecu.name] = ipcpluacontentRx

                    ipcpluaeculengthRx += parsesignallen(
                        str(signal.type).replace("TypeEnum.", ""))
                    ipcpluaecdlengthdictCEMRx1[ecu.name] = ipcpluaeculengthRx + ipcpluaeculengthCEMRx1

        if ecu.name == "HOD":
            strTemp = "HOD"+"RX"
            HOD_service_id.append('    [%d] = "%s"' %
                                  (ecu.serviceId, ecu.name))
            for signal in ecu.rxPackage.signals:
                index = index+1
                HOD_operation_id.append("    [0x%02x%02x] = \"%s\"," % (
                    ecu.serviceId, index, signal.name))
                SignalDefinition.append("    "+'["'+signal.name + '"] = ProtoField.' + str(signal.type).replace(
                    "_t", "").replace("TypeEnum.", "")+'("IPCP_' + ecu.name + "_RX_Data"+'",'+'"'+signal.name+'"'+",base.DEX),")

                if ipcpluadictHODRx1:
                    ipcpluaeculengthHODRx1 = ipcpluaecdlengthdictHODRx1.get(
                        ecu.name)
                    ipcpluacontentRx.append("            subtree:add(signal_table[\"%s\"],buf(%s + %d,%d));\n" % (
                        signal.name, strTemp, ipcpluaeculengthHODRx1, parsesignallen(str(signal.type).replace("TypeEnum.", ""))))
                    ipcpluadictHODRx1[ecu.name] = ipcpluacontentRx

                    ipcpluaeculengthHODRx1 += parsesignallen(
                        str(signal.type).replace("TypeEnum.", ""))
                    ipcpluaecdlengthdictHODRx1[ecu.name] = ipcpluaeculengthHODRx1
                else:
                    ipcpluaeculengthHODRx1 = 0
                    ipcpluaeculengthHODRx1 += 6
                    ipcpluacontentRx.append("            subtree:add(signal_table[\"%s\"],buf(%s + %d,%d));\n" % (
                        signal.name, strTemp, ipcpluaeculengthHODRx1, parsesignallen(str(signal.type).replace("TypeEnum.", ""))))
                    ipcpluadictHODRx1[ecu.name] = ipcpluacontentRx

                    ipcpluaeculengthRx += parsesignallen(
                        str(signal.type).replace("TypeEnum.", ""))
                    ipcpluaecdlengthdictHODRx1[ecu.name] = ipcpluaeculengthRx + ipcpluaeculengthHODRx1

        if ecu.name == "HUD":
            strTemp = "HUD"+"RX"
            HUD_service_id.append('    [%d] = "%s"' %
                                  (ecu.serviceId, ecu.name))
            for signal in ecu.rxPackage.signals:
                index = index+1
                HUD_operation_id.append("    [0x%02x%02x] = \"%s\"," % (
                    ecu.serviceId, index, signal.name))
                SignalDefinition.append("    "+'["'+signal.name + '"] = ProtoField.' + str(signal.type).replace(
                    "_t", "").replace("TypeEnum.", "")+'("IPCP_' + ecu.name + "_RX_Data"+'",'+'"'+signal.name+'"'+",base.DEX),")

                if ipcpluadictHUDRx1:
                    ipcpluaeculengthHUDRx1 = ipcpluaecdlengthdictHUDRx1.get(
                        ecu.name)
                    ipcpluacontentRx.append("            subtree:add(signal_table[\"%s\"],buf(%s + %d,%d));\n" % (
                        signal.name, strTemp, ipcpluaeculengthHUDRx1, parsesignallen(str(signal.type).replace("TypeEnum.", ""))))
                    ipcpluadictHUDRx1[ecu.name] = ipcpluacontentRx

                    ipcpluaeculengthHUDRx1 += parsesignallen(
                        str(signal.type).replace("TypeEnum.", ""))
                    ipcpluaecdlengthdictHUDRx1[ecu.name] = ipcpluaeculengthHUDRx1
                else:
                    ipcpluaeculengthHUDRx1 = 0
                    ipcpluaeculengthHUDRx1 += 6
                    ipcpluacontentRx.append("            subtree:add(signal_table[\"%s\"],buf(%s + %d,%d));\n" % (
                        signal.name, strTemp, ipcpluaeculengthHUDRx1, parsesignallen(str(signal.type).replace("TypeEnum.", ""))))
                    ipcpluadictHUDRx1[ecu.name] = ipcpluacontentRx

                    ipcpluaeculengthRx += parsesignallen(
                        str(signal.type).replace("TypeEnum.", ""))
                    ipcpluaecdlengthdictHUDRx1[ecu.name] = ipcpluaeculengthRx + ipcpluaeculengthHUDRx1

        if ecu.name == "SRS":
            strTemp = "SRS"+"RX"
            SRS_service_id.append('    [%d] = "%s"' %
                                  (ecu.serviceId, ecu.name))
            for signal in ecu.rxPackage.signals:
                index = index+1
                SRS_operation_id.append("    [0x%02x%02x] = \"%s\"," % (
                    ecu.serviceId, index, signal.name))
                SignalDefinition.append("    "+'["'+signal.name + '"] = ProtoField.' + str(signal.type).replace(
                    "_t", "").replace("TypeEnum.", "")+'("IPCP_' + ecu.name + "_RX_Data"+'",'+'"'+signal.name+'"'+",base.DEX),")

                if ipcpluadictSRSRx1:
                    ipcpluaeculengthSRSRx1 = ipcpluaecdlengthdictSRSRx1.get(
                        ecu.name)
                    ipcpluacontentRx.append("            subtree:add(signal_table[\"%s\"],buf(%s + %d,%d));\n" % (
                        signal.name, strTemp, ipcpluaeculengthSRSRx1, parsesignallen(str(signal.type).replace("TypeEnum.", ""))))
                    ipcpluadictSRSRx1[ecu.name] = ipcpluacontentRx

                    ipcpluaeculengthSRSRx1 += parsesignallen(
                        str(signal.type).replace("TypeEnum.", ""))
                    ipcpluaecdlengthdictSRSRx1[ecu.name] = ipcpluaeculengthSRSRx1
                else:
                    ipcpluaeculengthSRSRx1 = 0
                    ipcpluaeculengthSRSRx1 += 6
                    ipcpluacontentRx.append("            subtree:add(signal_table[\"%s\"],buf(%s + %d,%d));\n" % (
                        signal.name, strTemp, ipcpluaeculengthSRSRx1, parsesignallen(str(signal.type).replace("TypeEnum.", ""))))
                    ipcpluadictSRSRx1[ecu.name] = ipcpluacontentRx

                    ipcpluaeculengthRx += parsesignallen(
                        str(signal.type).replace("TypeEnum.", ""))
                    ipcpluaecdlengthdictSRSRx1[ecu.name] = ipcpluaeculengthRx + ipcpluaeculengthSRSRx1

        if ecu.name == "VDDM":
            strTemp = "VDDM"+"RX"
            VDDM_service_id.append('    [%d] = "%s"' %
                                   (ecu.serviceId, ecu.name))
            for signal in ecu.rxPackage.signals:
                index = index+1
                VDDM_operation_id.append("    [0x%02x%02x] = \"%s\"," % (
                    ecu.serviceId, index, signal.name))
                SignalDefinition.append("    "+'["'+signal.name + '"] = ProtoField.' + str(signal.type).replace(
                    "_t", "").replace("TypeEnum.", "")+'("IPCP_' + ecu.name + "_RX_Data"+'",'+'"'+signal.name+'"'+",base.DEX),")

                if ipcpluadictVDDMRx1:
                    ipcpluaeculengthVDDMRx1 = ipcpluaecdlengthdictVDDMRx1.get(
                        ecu.name)
                    ipcpluacontentRx.append("            subtree:add(signal_table[\"%s\"],buf(%s + %d,%d));\n" % (
                        signal.name, strTemp, ipcpluaeculengthVDDMRx1, parsesignallen(str(signal.type).replace("TypeEnum.", ""))))
                    ipcpluadictVDDMRx1[ecu.name] = ipcpluacontentRx

                    ipcpluaeculengthVDDMRx1 += parsesignallen(
                        str(signal.type).replace("TypeEnum.", ""))
                    ipcpluaecdlengthdictVDDMRx1[ecu.name] = ipcpluaeculengthVDDMRx1
                else:
                    ipcpluaeculengthVDDMRx1 = 0
                    ipcpluaeculengthVDDMRx1 += 6
                    ipcpluacontentRx.append("            subtree:add(signal_table[\"%s\"],buf(%s + %d,%d));\n" % (
                        signal.name, strTemp, ipcpluaeculengthVDDMRx1, parsesignallen(str(signal.type).replace("TypeEnum.", ""))))
                    ipcpluadictVDDMRx1[ecu.name] = ipcpluacontentRx

                    ipcpluaeculengthRx += parsesignallen(
                        str(signal.type).replace("TypeEnum.", ""))
                    ipcpluaecdlengthdictVDDMRx1[ecu.name] = ipcpluaeculengthRx + ipcpluaeculengthVDDMRx1

        if ecu.name == "CDM":
            strTemp = "CDM"+"RX"
            CDM_service_id.append('    [%d] = "%s"' %
                                   (ecu.serviceId, ecu.name))
            for signal in ecu.rxPackage.signals:
                index = index+1
                CDM_operation_id.append("    [0x%02x%02x] = \"%s\"," % (
                    ecu.serviceId, index, signal.name))
                SignalDefinition.append("    "+'["'+signal.name + '"] = ProtoField.' + str(signal.type).replace(
                    "_t", "").replace("TypeEnum.", "")+'("IPCP_' + ecu.name + "_RX_Data"+'",'+'"'+signal.name+'"'+",base.DEX),")

                if ipcpluadictCDMRx1:
                    ipcpluaeculengthCDMRx1 = ipcpluaecdlengthdictCDMRx1.get(
                        ecu.name)
                    ipcpluacontentRx.append("            subtree:add(signal_table[\"%s\"],buf(%s + %d,%d));\n" % (
                        signal.name, strTemp, ipcpluaeculengthCDMRx1, parsesignallen(str(signal.type).replace("TypeEnum.", ""))))
                    ipcpluadictCDMRx1[ecu.name] = ipcpluacontentRx

                    ipcpluaeculengthCDMRx1 += parsesignallen(
                        str(signal.type).replace("TypeEnum.", ""))
                    ipcpluaecdlengthdictCDMRx1[ecu.name] = ipcpluaeculengthCDMRx1
                else:
                    ipcpluaeculengthCDMRx1 = 0
                    ipcpluaeculengthCDMRx1 += 6
                    ipcpluacontentRx.append("            subtree:add(signal_table[\"%s\"],buf(%s + %d,%d));\n" % (
                        signal.name, strTemp, ipcpluaeculengthCDMRx1, parsesignallen(str(signal.type).replace("TypeEnum.", ""))))
                    ipcpluadictCDMRx1[ecu.name] = ipcpluacontentRx

                    ipcpluaeculengthRx += parsesignallen(
                        str(signal.type).replace("TypeEnum.", ""))
                    ipcpluaecdlengthdictCDMRx1[ecu.name] = ipcpluaeculengthRx + ipcpluaeculengthCDMRx1

        if ecu.name == "CCSM":
            strTemp = "CCSM"+"RX"
            CCSM_service_id.append('    [%d] = "%s"' %
                                   (ecu.serviceId, ecu.name))
            for signal in ecu.rxPackage.signals:
                index = index+1
                CCSM_operation_id.append("    [0x%02x%02x] = \"%s\"," % (
                    ecu.serviceId, index, signal.name))
                SignalDefinition.append("    "+'["'+signal.name + '"] = ProtoField.' + str(signal.type).replace(
                    "_t", "").replace("TypeEnum.", "")+'("IPCP_' + ecu.name + "_RX_Data"+'",'+'"'+signal.name+'"'+",base.DEX),")

                if ipcpluadictCCSMRx1:
                    ipcpluaeculengthCCSMRx1 = ipcpluaecdlengthdictCCSMRx1.get(
                        ecu.name)
                    ipcpluacontentRx.append("            subtree:add(signal_table[\"%s\"],buf(%s + %d,%d));\n" % (
                        signal.name, strTemp, ipcpluaeculengthCCSMRx1, parsesignallen(str(signal.type).replace("TypeEnum.", ""))))
                    ipcpluadictCCSMRx1[ecu.name] = ipcpluacontentRx

                    ipcpluaeculengthCCSMRx1 += parsesignallen(
                        str(signal.type).replace("TypeEnum.", ""))
                    ipcpluaecdlengthdictCCSMRx1[ecu.name] = ipcpluaeculengthCCSMRx1
                else:
                    ipcpluaeculengthCCSMRx1 = 0
                    ipcpluaeculengthCCSMRx1 += 6
                    ipcpluacontentRx.append("            subtree:add(signal_table[\"%s\"],buf(%s + %d,%d));\n" % (
                        signal.name, strTemp, ipcpluaeculengthCCSMRx1, parsesignallen(str(signal.type).replace("TypeEnum.", ""))))
                    ipcpluadictCCSMRx1[ecu.name] = ipcpluacontentRx

                    ipcpluaeculengthRx += parsesignallen(
                        str(signal.type).replace("TypeEnum.", ""))
                    ipcpluaecdlengthdictCCSMRx1[ecu.name] = ipcpluaeculengthRx + ipcpluaeculengthCCSMRx1
        #SWSM
        if ecu.name == "SWSM":
            strTemp = "SWSM"+"RX"
            SWSM_service_id.append('    [%d] = "%s"' %
                                   (ecu.serviceId, ecu.name))
            for signal in ecu.rxPackage.signals:
                index = index+1
                SWSM_operation_id.append("    [0x%02x%02x] = \"%s\"," % (
                    ecu.serviceId, index, signal.name))
                SignalDefinition.append("    "+'["'+signal.name + '"] = ProtoField.' + str(signal.type).replace(
                    "_t", "").replace("TypeEnum.", "")+'("IPCP_' + ecu.name + "_RX_Data"+'",'+'"'+signal.name+'"'+",base.DEX),")

                if ipcpluadictSWSMRx1:
                    ipcpluaeculengthSWSMRx1 = ipcpluaecdlengthdictSWSMRx1.get(
                        ecu.name)
                    ipcpluacontentRx.append("            subtree:add(signal_table[\"%s\"],buf(%s + %d,%d));\n" % (
                        signal.name, strTemp, ipcpluaeculengthSWSMRx1, parsesignallen(str(signal.type).replace("TypeEnum.", ""))))
                    ipcpluadictSWSMRx1[ecu.name] = ipcpluacontentRx

                    ipcpluaeculengthSWSMRx1 += parsesignallen(
                        str(signal.type).replace("TypeEnum.", ""))
                    ipcpluaecdlengthdictSWSMRx1[ecu.name] = ipcpluaeculengthSWSMRx1
                else:
                    ipcpluaeculengthSWSMRx1 = 0
                    ipcpluaeculengthSWSMRx1 += 6
                    ipcpluacontentRx.append("            subtree:add(signal_table[\"%s\"],buf(%s + %d,%d));\n" % (
                        signal.name, strTemp, ipcpluaeculengthSWSMRx1, parsesignallen(str(signal.type).replace("TypeEnum.", ""))))
                    ipcpluadictSWSMRx1[ecu.name] = ipcpluacontentRx

                    ipcpluaeculengthRx += parsesignallen(
                        str(signal.type).replace("TypeEnum.", ""))
                    ipcpluaecdlengthdictSWSMRx1[ecu.name] = ipcpluaeculengthRx + ipcpluaeculengthSWSMRx1
        #FPL
        if ecu.name == "FPL":
            strTemp = "FPL"+"RX"
            FPL_service_id.append('    [%d] = "%s"' %
                                   (ecu.serviceId, ecu.name))
            for signal in ecu.rxPackage.signals:
                index = index+1
                FPL_operation_id.append("    [0x%02x%02x] = \"%s\"," % (
                    ecu.serviceId, index, signal.name))
                SignalDefinition.append("    "+'["'+signal.name + '"] = ProtoField.' + str(signal.type).replace(
                    "_t", "").replace("TypeEnum.", "")+'("IPCP_' + ecu.name + "_RX_Data"+'",'+'"'+signal.name+'"'+",base.DEX),")

                if ipcpluadictFPLRx1:
                    ipcpluaeculengthFPLRx1 = ipcpluaecdlengthdictFPLRx1.get(
                        ecu.name)
                    ipcpluacontentRx.append("            subtree:add(signal_table[\"%s\"],buf(%s + %d,%d));\n" % (
                        signal.name, strTemp, ipcpluaeculengthFPLRx1, parsesignallen(str(signal.type).replace("TypeEnum.", ""))))
                    ipcpluadictFPLRx1[ecu.name] = ipcpluacontentRx

                    ipcpluaeculengthFPLRx1 += parsesignallen(
                        str(signal.type).replace("TypeEnum.", ""))
                    ipcpluaecdlengthdictFPLRx1[ecu.name] = ipcpluaeculengthFPLRx1
                else:
                    ipcpluaeculengthFPLRx1 = 0
                    ipcpluaeculengthFPLRx1 += 6
                    ipcpluacontentRx.append("            subtree:add(signal_table[\"%s\"],buf(%s + %d,%d));\n" % (
                        signal.name, strTemp, ipcpluaeculengthFPLRx1, parsesignallen(str(signal.type).replace("TypeEnum.", ""))))
                    ipcpluadictFPLRx1[ecu.name] = ipcpluacontentRx

                    ipcpluaeculengthRx += parsesignallen(
                        str(signal.type).replace("TypeEnum.", ""))
                    ipcpluaecdlengthdictFPLRx1[ecu.name] = ipcpluaeculengthRx + ipcpluaeculengthFPLRx1
        # rx2
        if ecu.name == "VGM":
            strTemp = "VGM"+"RX"
            VGM_service_id.append('    [%d] = "%s"' %
                                  (ecu.serviceId, ecu.name))
            for signal in ecu.rxPackage.signals:
                index = index+1
                VGM_operation_id.append("    [0x%02x%02x] = \"%s\"," % (
                    ecu.serviceId, index, signal.name))
                SignalDefinition.append("    "+'["'+signal.name + '"] = ProtoField.' + str(signal.type).replace(
                    "_t", "").replace("TypeEnum.", "")+'("IPCP_' + ecu.name + "_RX_Data"+'",'+'"'+signal.name+'"'+",base.DEX),")

                if ipcpluadictVGMRx2:
                    ipcpluaeculengthVGMRx2 = ipcpluaecdlengthdictVGMRx2.get(
                        ecu.name)
                    ipcpluacontentRx.append("            subtree:add(signal_table[\"%s\"],buf(%s + %d,%d));\n" % (
                        signal.name, strTemp, ipcpluaeculengthVGMRx2, parsesignallen(str(signal.type).replace("TypeEnum.", ""))))
                    ipcpluadictVGMRx2[ecu.name] = ipcpluacontentRx

                    ipcpluaeculengthVGMRx2 += parsesignallen(
                        str(signal.type).replace("TypeEnum.", ""))
                    ipcpluaecdlengthdictVGMRx2[ecu.name] = ipcpluaeculengthVGMRx2
                else:
                    ipcpluaeculengthVGMRx2 = 16
                    ipcpluaeculengthVGMRx2 += 6
                    ipcpluacontentRx.append("    subtree:add(signal_table[\"%s\"],buf(%s + %d,%d));\n" % (
                        signal.name, strTemp, ipcpluaeculengthVGMRx2, parsesignallen(str(signal.type).replace("TypeEnum.", ""))))
                    ipcpluadictVGMRx2[ecu.name] = ipcpluacontentRx

                    ipcpluaeculengthRx += parsesignallen(
                        str(signal.type).replace("TypeEnum.", ""))
                    ipcpluaecdlengthdictVGMRx2[ecu.name] = ipcpluaeculengthRx + ipcpluaeculengthVGMRx2
        
        # CarConfig
        if ecu.name == "CARCONFIG":
            strTemp = "CARCONFIG"+"RX"
            CC_service_id.append('    [%d] = "%s"' %
                                  (ecu.serviceId, ecu.name))
            for signal in ecu.rxPackage.signals:
                index = index+1
                CC_operation_id.append("    [0x%02x%02x] = \"%s\"," % (
                    ecu.serviceId, index, signal.name))
                SignalDefinition.append("    "+'["'+signal.name + '"] = ProtoField.' + str(signal.type).replace(
                    "_t", "").replace("TypeEnum.", "")+'("IPCP_' + ecu.name + "_RX_Data"+'",'+'"'+signal.name+'"'+",base.DEX),")

                if ipcpluadictCCRx:
                    ipcpluaeculengthCCRx = ipcpluaecdlengthdictCCRx.get(
                        ecu.name)
                    #print(">>>>CC sigName:%s type:%s len:%d" % (signal.name,str(signal.type),  ipcpluaeculengthCCRx))
                    ipcpluacontentRx.append("            subtree:add(signal_table[\"%s\"],buf(%s + %d,%d));\n" % (
                        signal.name, strTemp, ipcpluaeculengthCCRx, parsesignallen(str(signal.type).replace("TypeEnum.", ""))))
                    ipcpluadictCCRx[ecu.name] = ipcpluacontentRx

                    ipcpluaeculengthCCRx += parsesignallen(
                        str(signal.type).replace("TypeEnum.", ""))
                    ipcpluaecdlengthdictCCRx[ecu.name] = ipcpluaeculengthCCRx
                else:
                    ipcpluaeculengthCCRx = 16
                    ipcpluacontentRx.append("    subtree:add(signal_table[\"%s\"],buf(%s + %d,%d));\n" % (
                        signal.name, strTemp, ipcpluaeculengthCCRx, parsesignallen(str(signal.type).replace("TypeEnum.", ""))))
                    ipcpluadictCCRx[ecu.name] = ipcpluacontentRx

                    ipcpluaeculengthRx += parsesignallen(
                        str(signal.type).replace("TypeEnum.", ""))
                    ipcpluaecdlengthdictCCRx[ecu.name] = ipcpluaeculengthRx + ipcpluaeculengthCCRx
        # CarConfig1000
        if ecu.name == "CARCONFIGTHOUSAND":
            strTemp = "CARCONFIGTHOUSAND"+"RX"
            CC1000_service_id.append('    [%d] = "%s"' %
                                  (ecu.serviceId, ecu.name))
            CC1000_service_id.append('')
            for signal in ecu.rxPackage.signals:
                index = index+1
                CC1000_operation_id.append("    [0x%02x%02x] = \"%s\"," % (
                    ecu.serviceId, index, signal.name))
                SignalDefinition.append("    "+'["'+signal.name + '"] = ProtoField.' + str(signal.type).replace(
                    "_t", "").replace("TypeEnum.", "")+'("IPCP_' + ecu.name + "_RX_Data"+'",'+'"'+signal.name+'"'+",base.DEX),")

                if ipcpluadictCC1000Rx:
                    ipcpluaeculengthCC1000Rx = ipcpluaecdlengthdictCC1000Rx.get(
                        ecu.name)
                    #print(">>>>CC1000 sigName:%s type:%s len:%d" % (signal.name,str(signal.type),  ipcpluaeculengthCC1000Rx))
                    ipcpluacontentRx.append("            subtree:add(signal_table[\"%s\"],buf(%s + %d,%d));\n" % (
                        signal.name, strTemp, ipcpluaeculengthCC1000Rx, parsesignallen(str(signal.type).replace("TypeEnum.", ""))))
                    ipcpluadictCC1000Rx[ecu.name] = ipcpluacontentRx

                    ipcpluaeculengthCC1000Rx += parsesignallen(
                        str(signal.type).replace("TypeEnum.", ""))
                    ipcpluaecdlengthdictCC1000Rx[ecu.name] = ipcpluaeculengthCC1000Rx
                else:
                    ipcpluaeculengthCC1000Rx = 16
                    ipcpluacontentRx.append("    subtree:add(signal_table[\"%s\"],buf(%s + %d,%d));\n" % (
                        signal.name, strTemp, ipcpluaeculengthCC1000Rx, parsesignallen(str(signal.type).replace("TypeEnum.", ""))))
                    ipcpluadictCC1000Rx[ecu.name] = ipcpluacontentRx

                    ipcpluaeculengthRx += parsesignallen(
                        str(signal.type).replace("TypeEnum.", ""))
                    ipcpluaecdlengthdictCC1000Rx[ecu.name] = ipcpluaeculengthRx + ipcpluaeculengthCC1000Rx

    # RX1
    ASDMList = ipcpluadictASDMRx1.get("ASDM")
    ASDMListLen = ipcpluaecdlengthdictASDMRx1.get("ASDM")

    AUDList = ipcpluadictAUDRx1.get("AUD")
    AUDListLen = ipcpluaecdlengthdictAUDRx1.get("AUD")

    BBMList = ipcpluadictBBMRx1.get("BBM")
    BBMListLen = ipcpluaecdlengthdictBBMRx1.get("BBM")

    CEMList = ipcpluadictCEMRx1.get("CEM")
    CEMListLen = ipcpluaecdlengthdictCEMRx1.get("CEM")

    HODList = ipcpluadictHODRx1.get("HOD")
    HODListLen = ipcpluaecdlengthdictHODRx1.get("HOD")

    HUDList = ipcpluadictHUDRx1.get("HUD")
    HUDListLen = ipcpluaecdlengthdictHUDRx1.get("HUD")

    SRSList = ipcpluadictSRSRx1.get("SRS")
    SRSListLen = ipcpluaecdlengthdictSRSRx1.get("SRS")

    VDDMList = ipcpluadictVDDMRx1.get("VDDM")
    VDDMListLen = ipcpluaecdlengthdictVDDMRx1.get("VDDM")

    CDMList = ipcpluadictCDMRx1.get("CDM")
    CDMListLen = ipcpluaecdlengthdictCDMRx1.get("CDM")

    CCSMList = ipcpluadictCCSMRx1.get("CCSM")
    CCSMListLen = ipcpluaecdlengthdictCCSMRx1.get("CCSM")

    SWSMList = ipcpluadictSWSMRx1.get("SWSM")
    SWSMListLen = ipcpluaecdlengthdictSWSMRx1.get("SWSM")

    VGMList = ipcpluadictVGMRx2.get("VGM")
    VGMListLen = ipcpluaecdlengthdictVGMRx2.get("VGM")

    CCList = ipcpluadictCCRx.get("CARCONFIG")
    CCListLen = ipcpluaecdlengthdictCCRx.get("CARCONFIG")

    CC1000List = ipcpluadictCC1000Rx.get("CARCONFIGTHOUSAND")
    CC1000ListLen = ipcpluaecdlengthdictCC1000Rx.get("CARCONFIGTHOUSAND")

    FPLList = ipcpluadictFPLRx1.get("FPL")
    FPLListLen = ipcpluaecdlengthdictFPLRx1.get("FPL")

    # rx1
    for i in range(len(ASDMList)):
        strTemp = str(ASDMList[i]).replace("ASDMRX", str("0"))
        wssignalrxstrucerx1.append(strTemp)

    lenAUD = ASDMListLen
    for i in range(len(AUDList)):
        strTemp = str(AUDList[i]).replace("AUDRX", str(lenAUD))
        wssignalrxstrucerx1.append(strTemp)

    lenBBM = lenAUD+AUDListLen
    for i in range(len(BBMList)):
        strTemp = str(BBMList[i]).replace("BBMRX", str(lenBBM))
        wssignalrxstrucerx1.append(strTemp)

    lenCEM = lenBBM+BBMListLen
    for i in range(len(CEMList)):
        strTemp = str(CEMList[i]).replace("CEMRX", str(lenCEM))
        wssignalrxstrucerx1.append(strTemp)

    lenHOD = lenCEM+CEMListLen
    for i in range(len(HODList)):
        strTemp = str(HODList[i]).replace("HODRX", str(lenHOD))
        wssignalrxstrucerx1.append(strTemp)

    lenHUD = lenHOD+HODListLen
    for i in range(len(HUDList)):
        strTemp = str(HUDList[i]).replace("HUDRX", str(lenHUD))
        wssignalrxstrucerx1.append(strTemp)

    lenSRS = lenHUD + HUDListLen
    for i in range(len(SRSList)):
        strTemp = str(SRSList[i]).replace("SRSRX", str(lenSRS))
        wssignalrxstrucerx1.append(strTemp)

    lenVDDM = lenSRS + SRSListLen
    for i in range(len(VDDMList)):
        strTemp = str(VDDMList[i]).replace("VDDMRX", str(lenVDDM))
        wssignalrxstrucerx1.append(strTemp)

    lenCDM = lenVDDM+VDDMListLen
    for i in range(len(CDMList)):
        strTemp = str(CDMList[i]).replace("CDMRX", str(lenCDM))
        wssignalrxstrucerx1.append(strTemp)
    
    lenCCSM = lenCDM+CDMListLen
    for i in range(len(CCSMList)):
        strTemp = str(CCSMList[i]).replace("CCSMRX", str(lenCCSM))
        wssignalrxstrucerx1.append(strTemp)
    
    lenSWSM = lenCCSM+CCSMListLen
    for i in range(len(SWSMList)):
        strTemp = str(SWSMList[i]).replace("SWSMRX", str(lenSWSM))
        wssignalrxstrucerx1.append(strTemp)

    lenFPL = lenSWSM+FPLListLen
    for i in range(len(FPLList)):
        strTemp = str(FPLList[i]).replace("FPLRX", str(lenFPL))
        wssignalrxstrucerx1.append(strTemp)

    # rx2
    for i in range(len(VGMList)):
        strTemp = str(VGMList[i]).replace("VGMRX", str(0))
        wssignalrxstrucerx2.append(strTemp)
    
    # CarConfig
    for i in range(len(CCList)):
        strTemp = str(CCList[i]).replace("CARCONFIGRX", str(0))
        wssignalrxstrucecc.append(strTemp)
    
    # CarConfig1000
    for i in range(len(CC1000List)):
        strTemp = str(CC1000List[i]).replace("CARCONFIGTHOUSANDRX", str(0))
        wssignalrxstrucecc1000.append(strTemp)

    rx_service_id.extend(ASDM_service_id)
    rx_service_id.extend(AUD_service_id)
    rx_service_id.extend(BBM_service_id)
    rx_service_id.extend(CEM_service_id)
    rx_service_id.extend(HOD_service_id)
    rx_service_id.extend(HUD_service_id)
    rx_service_id.extend(SRS_service_id)
    rx_service_id.extend(VDDM_service_id)
    rx_service_id.extend(CDM_service_id)
    rx_service_id.extend(CCSM_service_id)
    rx_service_id.extend(SWSM_service_id)
    rx_service_id.extend(VGM_service_id)
    rx_service_id.extend(FPL_service_id)
    rx_service_id.extend(CC_service_id)
    rx_service_id.extend(CC1000_service_id)

    rx_operation_id.extend(ASDM_operation_id)
    rx_operation_id.extend(AUD_operation_id)
    rx_operation_id.extend(BBM_operation_id)
    rx_operation_id.extend(CEM_operation_id)
    rx_operation_id.extend(HOD_operation_id)
    rx_operation_id.extend(HUD_operation_id)
    rx_operation_id.extend(SRS_operation_id)
    rx_operation_id.extend(VDDM_operation_id)
    rx_operation_id.extend(CDM_operation_id)
    rx_operation_id.extend(CCSM_operation_id)
    rx_operation_id.extend(SWSM_operation_id)
    rx_operation_id.extend(VGM_operation_id)
    rx_operation_id.extend(FPL_operation_id)
    rx_operation_id.extend(CC_operation_id)
    rx_operation_id.extend(CC1000_operation_id)

    saveluafile(out_path)


def parsesignallen(type):

    if (type == "uint8_t" or type == "int8_t"):
        bytenum = 1
    elif (type == "uint16_t" or type == "int16_t"):
        bytenum = 2
    elif (type == "uint32_t" or type == "int32_t") :
        bytenum = 4
    else:
        bytenum = 4
    return bytenum


def parseXML(path):
    with codecs.open(path, "r", encoding="utf-8") as xmlFile:
        xmlStr = xmlFile.read()
        convertDict = xmltodict.parse(xmlStr)

    return convertDict


def saveluafile(out_path: str):

    ipcpluaList.append(IPCP_lua.substitute(
        SIGNALSERVICE=",\n".join(rx_service_id),
        CBPASERVICE=",\n".join(cbpa_service_id),
        CBPAOPE="\n".join(cbpa_operation_id),
        SIGNALOPE="\n".join(rx_operation_id),
        SignalDefinition="\n".join(SignalDefinition),
        SignalRX1Struct="\n".join(wssignalrxstrucerx1),
        SignalRX2Struct="\n".join(wssignalrxstrucerx2),
        SignalCCStruct="\n".join(wssignalrxstrucecc),
        SignalCC1000Struct="\n".join(wssignalrxstrucecc1000),
        PaDefinition="\n".join(allPaDefinition),
        PAVINVinNumRX2Struct="\n".join(ipcpluaPAVINVinNumcontentRx),
        PAVPVersionRX2Struct="\n".join(ipcpluaPAVPVersioncontentRx),
        PACLRX2Struct="\n".join(ipcpluaPACLcontentRx),
        PAARWarningVloRX2Struct="\n".join(ipcpluaPAARWarningVlocontentRx),
        PAVFCRX2Struct="\n".join(ipcpluaPAVFCcontentRx),
        PAFDRX2Struct="\n".join(ipcpluaPAFDcontentRx),
        PAPowerResRX2Struct="\n".join(ipcpluaPAPowerRescontentRx),
        PADiagProxyRX2Struct="\n".join(ipcpluaPADiagProxycontentRx),
        PAFEFDSendVFCIdRX2Struct="\n".join(
                    ipcpluaPAFEFDSendVFCIdcontentRx),
        PAVINDiffMsgRX2Struct="\n".join(ipcpluaPAVINDiffMsgcontentRx),
        PAMcuLogPanicRX2Struct="\n".join(ipcpluaPAMcuLogPaniccontentRx),
        PAAmpDiagResultRX2Struct="\n".join(
            ipcpluaPAAmpDiagResultcontentRx),
        PAErrorReportRX2Struct="\n".join(ipcpluaPAErrorReportcontentRx),
        # save header
        FirstVINVinNum="\n".join(PAVINNumHeadList),
        FirstVPVersion="\n".join(PAVPVerHeadList),
        FirstCL="\n".join(PACLHeadList),
        FirstARWarningVlo="\n".join(PAARWarningHeadList),
        FirstVFC="\n".join(PAVFCHeadList),
        FirstFD="\n".join(PAFDHeadList),
        FirstErrorReport="\n".join(PAErrorReportHeadList),
        FirstPowerRes="\n".join(PAPOWERResHeadList),
        FirstDiagProxy="\n".join(PADiagProxyHeadList),
        FirstFEFDSendVFCId="\n".join(PAFEFDHeadList),
        FirstVINDiffMsg="\n".join(PAVINDiffMsgHeadList),
        FirstMcuLogPanic="\n".join(PAMcuLogHeadList),
        FirstAmpDiagResult="\n".join(PAAmpDiagResultHeadList),

    ))
    writeFile(os.path.join(out_path,"lua"),"apvp.lua",lambda file:file.write("".join(ipcpluaList)))
